package com.xiaoyao.novel.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xiaoyao.novel.dao.CategoryDao;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Category;

public class CategoryService extends BaseService<Category> {

	static CategoryDao dao = new CategoryDao();

	public CategoryService() {
		super(dao);
	}

	static List<Category> list = null;

	@Override
	public List<Category> getAll() {
		if (list==null) {
			list= super.getAll();
		}
		return list ;
	}
	@Override
	public void insert(Category object) {
		super.insert(object);
		list=super.getAll();
	}

	@Override
	public void update(Category object) {
		super.update(object);
		list=super.getAll();
	}
	@Override
	public void delete(Object id) {
		super.delete(id);
		list=super.getAll(); 
	}
	public List<Book> getCategoryBooks(int cateId) {
		List<Book> books = new ArrayList<Book>();
		try {
			books=dao.getCategoryBooks(cateId);
		} catch (SQLException e) {
//			e.printStackTrace();
			logger.error(e);
		}
		return books;
	}
	public void updateBookCategory(Category  category) {
		try {
			 dao.updateBookCategory(category);
		} catch (SQLException e) { 
			logger.error(e);
		}
	}
	public List<Book> listCount(int cateId,int bookCount) {
		List<Book> books = new ArrayList<Book>();
		try {
			Map<String, Object> map= new HashMap<String, Object>() ;
			map.put("cateId", cateId) ;
			map.put("bookCount",bookCount) ;
			books=dao.listCount(map);
		} catch (SQLException e) {
//			e.printStackTrace();
			logger.error(e);
		}
		return books;
	}

}
